Added reset method to Opts so that create.parseCommandLine can be used more than
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 21 Nov 2005 12:00:36 +0000 (13:00 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 21 Nov 2005 12:00:36 +0000 (13:00 +0100)
once.  This makes it possible to unit test this code.  Remove the odd aliasing
of gopts to opts inside parseCommandLine.

Rename local variables globals and locals inside opts.load -- these are built-in
functions.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xm/create.py
tools/python/xen/xm/opts.py

index 23ea2ba97504bd09d4c6d013d4997b877523780f..49a95ba277afdb399c1bc5bda475a5ee9cdf3eb1 100644 (file)
@@ -879,33 +879,33 @@ def balloon_out(dom0_min_mem, opts):
 
 
 def parseCommandLine(argv):
-    opts = gopts
-    args = opts.parse(argv)
-    if opts.vals.help:
-        opts.usage()
-    if opts.vals.help or opts.vals.help_config:
-        opts.load_defconfig(help=1)
-    if opts.vals.help or opts.vals.help_config:
+    gopts.reset()
+    args = gopts.parse(argv)
+    if gopts.vals.help:
+        gopts.usage()
+    if gopts.vals.help or gopts.vals.help_config:
+        gopts.load_defconfig(help=1)
+    if gopts.vals.help or gopts.vals.help_config:
         return (None, None)
 
-    if not opts.vals.display:
-        opts.vals.display = os.getenv("DISPLAY")
+    if not gopts.vals.display:
+        gopts.vals.display = os.getenv("DISPLAY")
 
     # Process remaining args as config variables.
     for arg in args:
         if '=' in arg:
             (var, val) = arg.strip().split('=', 1)
             gopts.setvar(var.strip(), val.strip())
-    if opts.vals.config:
-        config = opts.vals.config
+    if gopts.vals.config:
+        config = gopts.vals.config
     else:
-        opts.load_defconfig()
-        preprocess(opts.vals)
-        if not opts.getopt('name') and opts.getopt('defconfig'):
-            opts.setopt('name', os.path.basename(opts.getopt('defconfig')))
-        config = make_config(opts.vals)
+        gopts.load_defconfig()
+        preprocess(gopts.vals)
+        if not gopts.getopt('name') and gopts.getopt('defconfig'):
+            gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
+        config = make_config(gopts.vals)
 
-    return (opts, config)
+    return (gopts, config)
 
 
 def main(argv):
index 321e6783da9ebb0c41ea77cbc8abd6996c97820a..3f983407fc439d71711ad7d6095f26325eafd557 100644 (file)
@@ -60,6 +60,14 @@ class Opt:
         self.value = None
         self.set(default)
 
+
+    def reset(self):
+        self.specified_opt = None
+        self.specified_val = None
+        self.value = None
+        self.set(self.default)
+
+
     def __repr__(self):
         return self.name + '=' + str(self.specified_val)
 
@@ -223,6 +231,14 @@ class Opts:
         # Option to use for bare words.
         self.default_opt = None
 
+
+    def reset(self):
+        self.vals = OptVals()
+        self.vars = {}
+        for opt in self.options:
+            opt.reset()
+
+
     def __repr__(self):
         return '\n'.join(map(str, self.options))
 
@@ -416,22 +432,22 @@ class Opts:
         are used to set options with the same names.
         Variables are not used to set options that are already specified.
         """
-        # Create global and lobal dicts for the file.
+        # Create global and local dicts for the file.
         # Initialize locals to the vars.
         # Use exec to do the standard imports and
         # define variables we are passing to the script.
-        globals = {}
-        locals = {}
-        locals.update(self.vars)
+        globs = {}
+        locs = {}
+        locs.update(self.vars)
         cmd = '\n'.join(self.imports + 
                         [ "from xen.xm.help import Vars",
                           "xm_file = '%s'" % defconfig,
                           "xm_help = %d" % help,
                           "xm_vars = Vars(xm_file, xm_help, locals())"
                           ])
-        exec cmd in globals, locals
+        exec cmd in globs, locs
         try:
-            execfile(defconfig, globals, locals)
+            execfile(defconfig, globs, locs)
         except:
             if not help: raise
         if help:
@@ -444,7 +460,7 @@ class Opts:
                    types.IntType,
                    types.FloatType
                    ]
-        for (k, v) in locals.items():
+        for (k, v) in locs.items():
             if self.specified(k): continue
             if not(type(v) in vtypes): continue
             self.setopt(k, v)